package cn.tutu.blockchain.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.common.MsgObj;
import cn.tutu.blockchain.common.Page;
import cn.tutu.shop.entity.Express;
import cn.tutu.shop.entity.Order;
import cn.tutu.shop.entity.OrderItem;
import cn.tutu.shop.service.OrderService;

@RestController
@RequestMapping("/order")
public class OrderController {
	private static Logger LOGGER = LoggerFactory.getLogger(RegionController.class);
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public MsgObj create(@RequestBody List<OrderItem> items) {
		LOGGER.info("========创建订单信息:{}======", items.toString());
		orderService.create(items.get(0).getUserId(), items);
		return MsgObj.buildSuccessMsg();
	}

	@RequestMapping("/getGridByUser")
	public MsgObj getGrid(String userId, Page page) {

		LOGGER.info("========获取订单信息:{}======", userId);
		PageInfo<Order> info = orderService.getByUser(userId, page);

		return MsgObj.buildSuccessObj(info.getList());

	}

	@RequestMapping(value = "/createOnly", method = RequestMethod.POST)
	public MsgObj createOnly(@Valid OrderItem item, @Valid Express express) {
		LOGGER.info("========创建订单信息:{}======", item.toString());
		orderService.createOnly(item, express);
		return MsgObj.buildSuccessMsg();
	}
}
